
import { http } from "@kit.NetworkKit"

export interface Res<T>{
  code:number
  message:string
  data:T
}
let request = http.createHttp()
export function get<T>(url:string,data?:object):Promise<T>{
  return new Promise<T> ((resolve,reject)=>{
    if(data){
      url +='?'+Object.keys(data).map((k)=> `${k}=${data[k]}`).join('&')
    }
    request.request(url)
      .then((response)=>{
        if(response.responseCode == 200){

          let res:Res<T> = JSON.parse(response.result.toString())
          if(res.code == 200){
            resolve(res.data)
          }else{
            reject(new Error(res.message))
          }
        }else{
          reject(new Error(`网络错误：${response.responseCode}`))
        }
      })
      .catch((e:Error)=>{
        reject(e)
      })
  })
}
// let request = http.createHttp()
// export function get<weatherData>(url:string,data?:object):Promise<T>{
//   let request = http.createHttp()
//   request.request("http://192.168.110.39:8080/cart/getCarts?username=1",{method:http.RequestMethod.GET})
//     .then((response) =>{
//       console.log(`响应码：${response.responseCode}`)
//       if(response.responseCode == 200){
//         let res:Res<weatherData> = JSON.parse(response.result.toString())
//         this.message = `${res.message}\n
//                 商品id：${res.data[0].id}\n
//                 数量：${res.data[0].car_num}\n
//                 商品名${res.data[0].goods_desc}\n
//                 价钱${res.data[0].goods_price}\n
//                 图片网址${res.data[0].goods_icon}`
//         this.t= res.data[0].goods_icon
//         // Image(res.data[0].goods_icon)
//         //   .width('100%')
//         //   .height('100%')
//
//       }else{
//         this.message = "访问错误"
//       }
//
//     }).catch((e:Error)=>{
//     this.message = `出错了：${e.message}`
//
//   })
// }